<!DOCTYPE html>
<!--Copyright 2010 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.-->
<html>
<head>
  <title>Introducer Provider Menu</title>
  <script src="jsonrpc_client.js?providermenu" type="text/javascript"></script>
  <script type="text/javascript">
  function nada() {
    return false;
  }
  
  function getOrigin(l) {
    return l.protocol + "//" + l.host + (l.port && ":" + l.port);
  }
  
  function init() {
    var cwins;
    var choiceSelected = false;
    var responseReceived = false;
    var requestId = null;
    var myOrigin = getOrigin(window.location);
    var rpcclient;
    try {
      requestId = parseInt(document.location.search.substring(1), 10);
    } catch(e) {
      return;
    }
    
    try {
      cwins = window.parent.frames;
    } catch(e2) {
      return;
    }
    
    function setupProviderMenu(providers) {
      //TODO: Sort on use-frequency (maintain this info)
      var list = document.getElementById("providerList");
      var item;
      var selected = false;
      for (var i = providers.length; i--; ) {
        item = document.createElement("li");
        link = document.createElement("a");
        link.innerText = providers[i].name;
        link.textContent = providers[i].name;
        link.href = "javascript:nada();";
        link.onclick = (function(choice) {
          return function() {
            selectChoice(choice);
          };
        }(providers[i]));
        item.appendChild(link);
        list.appendChild(item);
      }
    }
    
    function selectChoice(choice) {
      if (choiceSelected) { return; }
      choiceSelected = true;
      //console.log(JSON.stringify(choice));
      if (choice.popup) {
        window.open(choice.URI, "" + requestId);
      }
      rpcclient.call("setChoosenProvider", [requestId, choice], function(x) {}, function(x) { console.log(x);});
    }
    
    var tempclient;
    for (var i = 0; i < cwins.length; i++) {
      try {
        if (cwins[i] !== window &&
            cwins[i].location.host === window.location.host &&
            cwins[i].location.protocol === window.location.protocol &&
            cwins[i].location.port === window.location.port) {
          tempclient = jsonrpc_client(getPostMessageSender(cwins[i], myOrigin)); // fill in
          tempclient.addMessageReceiver(getPostMessageReceiver(myOrigin, cwins[i]));
          tempclient.call("getProviderList", [requestId], (function(r) {
            return function(providers) {
              if (responseReceived) { return; }
              responseReceived = true;
              rpcclient = r;
              setupProviderMenu(providers);
            };
          }(tempclient)), function(error) { throw error; });
        }
      } catch(e) {}
    }
  }
  </script>
</head>
<body onload="init();">
  <ul id="providerList">
    
  </ul>
</body>
</html>